﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using VehicleMaintenance.Framework;
using VehicleMaintenance.UserControls;
using VehicleMaintenance.Properties;
using System.Threading;
using VehicleMaintenance.BaseInfo;
using VehicleMaintenance.Transaction;
using VehicleMaintenance.Query;
using VehicleMaintenance.Systems;
using VehicleMaintenance.Models;
using VehicleMaintenance.Comm;
using System.IO;
using System.Text.RegularExpressions;

namespace VehicleMaintenance
{
    public partial class MainForm2 : BaseForm
    {
        /// <summary>
        /// 记录当前登录用户
        /// </summary>
        public static Models.User User;

        public MainForm2()
        {
            InitializeComponent();
            InitializeElements();

            this.Shown += new EventHandler(MainForm2_Shown);
        }


        private void InitializeElements()
        {
            string[] strBase = System.Configuration.ConfigurationManager.AppSettings["BaseMenu"].Split(',');
            string[] strWork = System.Configuration.ConfigurationManager.AppSettings["WorkMenu"].Split(',');
            string[] strQuery = System.Configuration.ConfigurationManager.AppSettings["QueryMenu"].Split(',');

            //查找Image图像路径
            string appDirecroty = Application.StartupPath;
            DirectoryInfo dir = new DirectoryInfo(appDirecroty);
#if DEBUG
            string imageDirecroty = Path.Combine(dir.Parent.Parent.FullName, "Image");
#else
            string imageDirecroty = Path.Combine(dir.FullName, "Image");
#endif
            bool isExistImage = Directory.Exists(imageDirecroty);

            for (int i = 0; i < strBase.Length; i++)
            {
                ucElements element = new ucElements();
                if (strBase[i].Length > 0)
                {
                    if (!isExistImage)
                    {
                        element.Image = Resources.bagong_001;
                    }
                    else
                    {
                        Regex regex = new Regex(@"\[(\w+\.\w+)\]$");
                        Match match = regex.Match(strBase[i]);
                        if (match.Success)
                        {
                            string image = match.Groups[1].Value;
                            string imageFile = Path.Combine(imageDirecroty, image);
                            element.picBox.Image = Image.FromFile(imageFile);
                        }
                        else
                        {
                            element.picBox.Image = Resources.bagong_001;
                        }
                    }
                }
                else
                {
                    element.Cursor = Cursors.Default;
                }
                int index = strBase[i].IndexOf('[');
                element.Content = index > -1 ? strBase[i].Remove(index) : strBase[i];
                element.Margin = new Padding(30, 10, 30, 10);
                element.Click += new EventHandler(element_Click);
                tabLayoutLeft.Controls.Add(element);
            }
            for (int i = 0; i < strWork.Length; i++)
            {
                ucElements element = new ucElements();
                if (strWork[i].Length > 0)
                {
                    if (!isExistImage)
                    {
                        element.Image = Resources.bagong_001;
                    }
                    else
                    {
                        Regex regex = new Regex(@"\[(\w+\.\w+)\]$");
                        Match match = regex.Match(strWork[i]);
                        if (match.Success)
                        {
                            string image = match.Groups[1].Value;
                            string imageFile = Path.Combine(imageDirecroty, image);
                            element.picBox.Image = Image.FromFile(imageFile);
                        }
                        else
                        {
                            element.picBox.Image = Resources.bagong_001;
                        }
                    }
                }
                else
                {
                    element.Cursor = Cursors.Default;
                }
                int index = strWork[i].IndexOf('[');
                element.Content = index > -1 ? strWork[i].Remove(index) : strWork[i];
                element.Margin = new Padding(30, 10, 30, 10);
                element.Click += new EventHandler(element_Click);
                tabLayoutMiddle.Controls.Add(element);
            }
            for (int i = 0; i < strQuery.Length; i++)
            {
                ucElements element = new ucElements();
                if (strQuery[i].Length > 0)
                {
                    if (!isExistImage)
                    {
                        element.Image = Resources.bagong_001;
                    }
                    else
                    {
                        Regex regex = new Regex(@"\[(\w+\.\w+)\]$");
                        Match match = regex.Match(strQuery[i]);
                        if (match.Success)
                        {
                            string image = match.Groups[1].Value;
                            string imageFile = Path.Combine(imageDirecroty, image);
                            element.picBox.Image = Image.FromFile(imageFile);
                        }
                        else
                        {
                            element.picBox.Image = Resources.bagong_001;
                        }
                    }
                }
                else
                {
                    element.Cursor = Cursors.Default;
                }
                int index = strQuery[i].IndexOf('[');
                element.Content = index > -1 ? strQuery[i].Remove(index) : strQuery[i];
                element.Margin = new Padding(30, 10, 30, 10);
                element.Click += new EventHandler(element_Click);
                tabLayoutRight.Controls.Add(element);
            }
        }

        void element_Click(object sender, EventArgs e)
        {
            ucElements con = sender as ucElements;
            if (con != null)
            {
                switch (con.Content)
                {
                    #region 基础信息
                    case "店面信息":
                        {
                            BaseInfoForm form = new BaseInfo.BaseInfoForm();
                            form.Text = con.Content;
                            form.ShowInTaskbar = false;
                            form.ShowDialog(this);
                            break;
                        }
                    case "员工信息":
                        {
                            StaffListForm form = new StaffListForm(false);
                            form.Text = con.Content;
                            form.ShowInTaskbar = false;
                            form.ShowDialog(this);
                            break;
                        }
                    case "客户信息":
                        {
                            CustomerListForm form = new CustomerListForm(false);
                            form.Text = con.Content;
                            form.ShowInTaskbar = false;
                            form.ShowDialog(this);
                            break;
                        }
                    case "车辆档案":
                        {
                            CarListForm form = new CarListForm(false);
                            form.Text = con.Content;
                            form.ShowInTaskbar = false;
                            form.ShowDialog(this);
                            break;
                        }
                    case "商品信息":
                        {
                            GoodListForm form = new GoodListForm(false);
                            form.Text = con.Content;
                            form.ShowInTaskbar = false;
                            form.ShowDialog(this);
                            break;
                        }
                    case "商品类别":
                        {
                            GoodTypeForm form = new GoodTypeForm(false);
                            form.Text = con.Content;
                            form.ShowInTaskbar = false;
                            form.ShowDialog(this);
                            break;
                        }
                    case "维修类别":
                        {
                            MaintenanceCategoriesForm form = new MaintenanceCategoriesForm(false);
                            form.Text = con.Content;
                            form.ShowInTaskbar = false;
                            form.ShowDialog(this);
                            break;
                        }
                    case "车型管理":
                        {
                            BaseInfo.CarTypeForm form = new CarTypeForm(false);
                            form.Text = con.Content;
                            form.ShowInTaskbar = false;
                            form.ShowDialog(this);
                            break;
                        }
                    case "客户类别":
                        {
                            BaseInfo.CustomerTypeForm form = new CustomerTypeForm(false);
                            form.Text = con.Content;
                            form.ShowInTaskbar = false;
                            form.ShowDialog(this);
                            break;
                        }
                    case "系统管理":
                        {
                            Systems.SystemForm form = new Systems.SystemForm();
                            form.Text = con.Content;
                            form.ShowInTaskbar = false;
                            form.ShowDialog(this);
                            break;
                        }
                    case "日常开支":
                        {
                            OtherExpenditureListForm form = new OtherExpenditureListForm(false);
                            form.Text = con.Content;
                            form.ShowInTaskbar = false;
                            form.ShowDialog(this);
                            break;
                        }
                    #endregion 基础信息

                    #region 维修信息

                    case "库存信息":
                        {
                            InventoryForm form = new InventoryForm();
                            form.Text = con.Content;
                            form.ShowInTaskbar = false;
                            form.ShowDialog(this);
                            break;
                        }
                    case "商品入库":
                        {
                            PurchaseBillForm form = new PurchaseBillForm();
                            form.Text = con.Content;
                            form.ShowInTaskbar = false;
                            form.ShowDialog(this);
                            break;
                        }
                    case "商品报损":
                        {
                            DamageBillForm form = new DamageBillForm();
                            form.Text = con.Content;
                            form.ShowInTaskbar = false;
                            form.ShowDialog(this);
                            break;
                        }
                    case "库存退货":
                        {
                            PurchaseReturnsBillForm form = new PurchaseReturnsBillForm();
                            form.Text = con.Content;
                            form.ShowInTaskbar = false;
                            form.ShowDialog(this);
                            break;
                        }
                    case "商品销售":
                        {
                            SalesBillForm form = new SalesBillForm();
                            form.Text = con.Content;
                            form.ShowInTaskbar = false;
                            form.ShowDialog(this);
                            break;
                        }
                    case "商品退货":
                        {
                            SalseReturnsBillForm form = new SalseReturnsBillForm();
                            form.Text = con.Content;
                            form.ShowInTaskbar = false;
                            form.ShowDialog(this);
                            break;
                        }
                    case "维修登记":
                        {
                            MainBillForm form = new MainBillForm();
                            form.Text = con.Content;
                            form.ShowInTaskbar = false;
                            form.ShowDialog(this);
                            break;
                        }
                    case "开始维修":
                        {
                            WorkForm form = new WorkForm("已登记");
                            form.Text = con.Content;
                            form.ShowInTaskbar = false;
                            form.ShowDialog(this);
                            break;
                        }
                    case "维修结算":
                        {
                            WorkForm form = new WorkForm("修理中");
                            form.Text = con.Content;
                            form.ShowInTaskbar = false;
                            form.ShowDialog(this);
                            break;
                        }

                    #endregion 维修信息

                    #region 信息查询

                    case "应收帐款":
                        {
                            NeedChargedForSaleForm form = new NeedChargedForSaleForm();
                            form.Text = con.Content;
                            form.ShowInTaskbar = false;
                            form.ShowDialog(this);
                            break;
                        }
                    case "应付账款":
                        {
                            NeedChargedForBuyForm form = new NeedChargedForBuyForm();
                            form.Text = con.Content;
                            form.ShowInTaskbar = false;
                            form.ShowDialog(this);
                            break;
                        }
                    case "现金帐目":
                        {
                            CashAccountsForm form = new CashAccountsForm();
                            form.Text = con.Content;
                            form.ShowInTaskbar = false;
                            form.ShowDialog(this);
                            break;
                        }
                    case "客户查询":
                        {
                            CommonQueryForm form = new CommonQueryForm();
                            form.Text = con.Content;
                            form.ShowInTaskbar = false;
                            form.ShowDialog(this);
                            break;
                        }
                    case "维修单查询":
                        {
                            RecipientQueryForm form = new RecipientQueryForm();
                            form.Text = con.Content;
                            form.ShowInTaskbar = false;
                            form.ShowDialog(this);
                            break;
                        }
                    case "入库单查询":
                        {
                            PurchaseQueryForm form = new PurchaseQueryForm();
                            form.Text = con.Content;
                            form.ShowInTaskbar = false;
                            form.ShowDialog(this);
                            break;
                        }
                    case "报损单查询":
                        {
                            DamageQueryForm form = new DamageQueryForm();
                            form.Text = con.Content;
                            form.ShowInTaskbar = false;
                            form.ShowDialog(this);
                            break;
                        }
                    case "库存退货查询":
                        {
                            PurchaseReturnsQueryForm form = new PurchaseReturnsQueryForm();
                            form.Text = con.Content;
                            form.ShowInTaskbar = false;
                            form.ShowDialog(this);
                            break;
                        }
                    case "销售单查询":
                        {
                            SalesQueryForm form = new SalesQueryForm();
                            form.Text = con.Content;
                            form.ShowInTaskbar = false;
                            form.ShowDialog(this);
                            break;
                        }
                    case "销售退货查询":
                        {
                            SalseReturnsQueryForm form = new SalseReturnsQueryForm();
                            form.Text = con.Content;
                            form.ShowInTaskbar = false;
                            form.ShowDialog(this);
                            break;
                        }
                    #endregion 信息查询

                    default: break;
                }
            }
        }

        private void SwitchScreen(int index)
        {
            this.panel2.Enabled = false;
            switch (index)
            {
                case 1:
                    {
                        while (pnMiddle.Width > 0)
                        {
                            pnMiddle.Width -= 50;
                            pnMiddle.Left += 50;
                            pnLeft.Width += 50;
                            //Thread.Sleep(50);
                            Application.DoEvents();
                        }
                        while (pnRight.Width > 0)
                        {
                            pnRight.Width -= 50;
                            pnRight.Left += 50;
                            pnLeft.Width += 50;
                            //Thread.Sleep(50);
                            Application.DoEvents();
                        }
                        pnLeft.Left = 0;
                        break;
                    }
                case 2:
                    {
                        while (pnLeft.Width > 0)
                        {
                            pnLeft.Width -= 50;
                            pnMiddle.Width += 50;
                            pnMiddle.Left -= 50;
                            //Thread.Sleep(50);
                            Application.DoEvents();
                        }
                        while (pnRight.Width > 0)
                        {
                            pnRight.Width -= 50;
                            pnRight.Left += 50;
                            pnMiddle.Width += 50;
                            //Thread.Sleep(50);
                            Application.DoEvents();
                        }
                        pnMiddle.Left = 0;
                        break;
                    }
                case 3:
                    {
                        while (pnLeft.Width > 0)
                        {
                            pnLeft.Width -= 50;
                            pnRight.Width += 50;
                            pnRight.Left -= 50;
                            //Thread.Sleep(50);
                            Application.DoEvents();
                        }
                        while (pnMiddle.Width > 0)
                        {
                            pnMiddle.Width -= 50;
                            pnRight.Width += 50;
                            pnRight.Left -= 50;
                            //Thread.Sleep(50);
                            Application.DoEvents();
                        }
                        pnRight.Left = 0;
                        break;
                    }
            }
            this.panel2.Enabled = true;
        }

        private void MainForm2_Load(object sender, EventArgs e)
        {
            try
            {

                rbMiddle.Checked = false;
                rbMiddle.Checked = true;

                timer1_Tick(sender, e);
                timer1.Interval = 600000;
                timer1.Enabled = true;
            }
            catch (FileNotFoundException)
            {
                this.ShowInfo("程序文件被损坏，程序将退出");
                Application.Exit();
            }
            catch (Exception ex)
            {
                this.ShowInfo(ex.Message);
            }
        }

        void MainForm2_Shown(object sender, EventArgs e)
        {
            try
            {
                if (!RegisterInfo.IsRegistered())
                {
                    this.Text += "--试用版";
                    CarDBContext db = new CarDBContext();
                    int count = db.MainBill.Count();
                    this.ShowInfo(string.Format("您的软件尚未注册\r\n业务目前被限制为500笔,当前业务{0}笔\r\n升级至正版，请联系zyf364@gmail.com", count.ToString()));
                    if (count > 500)
                    {
                        this.panel1.Enabled = false;
                        this.panel2.Enabled = false;
                    }
                }
            }
            catch (FileNotFoundException)
            {
                this.ShowInfo("程序文件被损坏，程序将退出");
                Application.Exit();
            }
            catch (Exception ex)
            {
                this.ShowInfo(ex.Message);
            }
        }


        private void rbMiddle_CheckedChanged(object sender, EventArgs e)
        {
            if (rbMiddle.Checked)
            {
                SwitchScreen(2);
                lbTitle.Text = "修车管理";
            }
        }

        private void rbLeft_CheckedChanged(object sender, EventArgs e)
        {
            if (rbLeft.Checked)
            {
                SwitchScreen(1);
                lbTitle.Text = "基本信息";
            }
        }

        private void rbRight_CheckedChanged(object sender, EventArgs e)
        {
            if (rbRight.Checked)
            {
                SwitchScreen(3);
                lbTitle.Text = "查询统计";
            }
        }

        private void picRight_Click(object sender, EventArgs e)
        {
            if (rbLeft.Checked)
            {
                rbMiddle.Checked = true;
            }
            else if (rbMiddle.Checked)
            {
                rbRight.Checked = true;
            }
        }

        private void picLeft_Click(object sender, EventArgs e)
        {
            if (rbRight.Checked)
            {
                rbMiddle.Checked = true;
            }
            else if (rbMiddle.Checked)
            {
                rbLeft.Checked = true;
            }
        }

        private void MainForm2_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.Exit();
        }

        /// <summary>
        /// 10分钟检查一下库存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void timer1_Tick(object sender, EventArgs e)
        {
            CarDBContext db = new CarDBContext();
            var list = db.Goods.Where(p => p.Bottom > 0 && p.Number <= p.Bottom);//低于下限
            string str = "";
            if (list != null && list.Count() > 0)
            {
                str += "以下商品的库存不足：";
                foreach (var item in list)
                {
                    str += string.Format("\r\n{0}:当前库存{1}{2},库存底限{3}{2}", item.Name, item.Number, item.Units, item.Bottom);
                }

            }
            list = db.Goods.Where(p => p.Top > 0 && p.Number >= p.Top);//低于下限
            if (list != null && list.Count() > 0)
            {
                if (str.Length > 0)
                {
                    str += "\r\n\r\n";
                }
                str += "以下商品的库存超出上限：";
                foreach (var item in list)
                {
                    str += string.Format("\r\n{0}:当前库存{1}{2},库存上限{3}{2}", item.Name, item.Number, item.Units, item.Top);
                }
            }

            if (str.Length > 0)
            {
                AlertForm form = new AlertForm("库存报警", str);
                form.Show();
            }
        }
    }
}
